/*
 * @Author      : 徐博
 * @Date        : 2024-05-21 17:51:49
 * @LastEditTime: 2024-05-21 17:52:19
 * @LastEditors : 徐博
 * @FilePath    : /Users/xubo/study/vue3/vue3-ts-vite/src/router/index.ts
 * @Description : desc
 */
/*
 * @Author: 徐博
 * @Date: 2023-02-16 17:29:15
 * @Last Modified by: 徐博
 * @Last Modified time: 2023-03-07 15:35:19
 */

import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
import { message } from 'ant-design-vue';
import {GlobalState} from "@/store";

const Index = () => import('@/views/index/index.vue')
const NotPage = () => import('@/views/common/404.vue')
const IFRAME_PAGE = () => import('@/views/common/iframe.vue')
const routes: Array<RouteRecordRaw> =  [
  {
    path: "/",
    name: "home",
    meta: {
      title: "首页",
    },
    component: Index,
    children: [
      {
        path: '/userList',
        name: 'userList',
        meta: {
          title: '用户列表'
        },
        component: ()=>import('@/views/user/index.vue')
      },
      {
        path: '/userList1',
        name: 'userList1',
        meta: {
          title: '用户列表1'
        },
        component: ()=>import('@/views/user/index1.vue')
      },
      {
        path: '/userDetail',
        name: 'userDetail',
        meta: {
          title: '用户详情'
        },
        component: ()=>import('@/views/user/detail.vue')
      },
      {
        path: '/iframe',
        name: 'Iframe',
        meta: {
          title: '外部页面'
        },
        component: IFRAME_PAGE
      }
    ]
  },
  {
    path: '/404',
    name: '404',
    meta: {
      title: '404'
    },
    component: NotPage
  }
]
const router = createRouter({
    history: createWebHistory(),
    routes,
  });
//路由拦截
// 路由白名单 不需要权限的页面name
const WHITE_MENUS = ['/404','/login','/','/iframe']
router.beforeEach((to, form, next) => {
  // const token = window.sessionStorage.getItem('token');
  const store = GlobalState();
  const permissions_menu = store.userInfo.permissions_menu || [];

  // @ts-ignore
  if(!permissions_menu.includes(to.path) && !WHITE_MENUS.includes(to.path)) {
    // if(to.path == '/login') {
    //   next()
    //   return
    // }
    // message.error('当前登录已过期,请重新登录');
    next('/404')
  }else {
    next()
  }
})
export default router;
